#pragma once
#include "Scene.h"
#include "ImageBuffer.h"
#include "Intersection.h"
#include "Ray.h"
#include "BVH.h"
#define _USE_MATH_DEFINES
#include <math.h>
#include <time.h>

#include "Math4D.h"

class Renderer{
public:
	Renderer();
	~Renderer();

	void init();

	//prepare the scene for rendering
	//This will allow for an acceleration structure in the future
	bool prepare();

	//render the scene
	void render();

	int AAmode(){ return theAAmode; }
	void AAmode(int _mode){ theAAmode = _mode; }

	int AAsamples(){ return AAminSamples(); }
	void AAsamples(int _samples){ AAminSamples(_samples); }

	int AAsamplePattern(){ return samplePatternMode; }
	void AAsamplePattern(int _mode){ samplePatternMode = _mode; }

	int AAminSamples(){ return theAAminSamples;	}
	void AAminSamples(int _samples){ theAAminSamples = _samples; }

	int AAmaxSamples(){	return theAAmaxSamples;	}
	void AAmaxSamples(int _samples){ theAAmaxSamples = _samples; }

	int maxBounces(){ return theMaxBounces;	}
	void maxBounces(int _b){ theMaxBounces = _b; }

	void scene(Scene* _sc){ theScene = _sc; }
	void image(ImageBuffer*);
	ImageBuffer* image(){ return theImage; }
	void camera(Camera* _cam){ theCamera = _cam; }
	
	vec4 backgroundColor(){ return backgroundCol; }
	void backgroundColor(vec4 _col){ backgroundCol = _col; }

	void debugPixel(int _x, int _y, int _z);	//for debugging an individual pixel

	void clampMode(bool _c){ clampingEnabled = _c; }	//enable/disable color clamping

private:
	//render the scene with the same number of AA samples per pixel
	void renderUniformAA(int _samples);

	//render the scene with a dynamic number of AA samples per pixel
	//void renderDynamicAA(int _minSamples, int _maxSamples);

	//render a specific pixel with a dynamic number of AA samples
	void renderPixel(int _x, int _y, int _z, int _samples);

	//pointer to the scene
	Scene* theScene;

	//pointer to an image buffer
	ImageBuffer* theImage;

	//pointer to a camera
	Camera* theCamera;

	//has the scene been prepared to render?
	//This will allow for an acceleration structure in the future
	bool readyToRender;

	//0: no anti-aliasing
	//1: uniform number of AA samples per pixel
	//2: anti-alias edges (using an image gradient)
	int theAAmode;

	//0: grid anti-aliasing
	//1: jitter anti-aliasing
	int samplePatternMode;

	//the square root of the minimum number of samples that will be taken for each pixel
	int theAAminSamples;

	//the square root of the maximum number of samples that will be taken for each pixel
	//really only used for anti-alias edges mode
	int theAAmaxSamples;

	//the maximum number of bounces for this ray
	int theMaxBounces;		//defaults to 4

	//the number of path tracing samples to take
	//For when this becomes a path tracer
	//int pathSamples;

	//whether the image buffer was passed-in or created on the fly
	bool autoImageBuffer;
	
	//background color
	vec4 backgroundCol;

	BVH theBVH;

	bool clampingEnabled;	//whether color clamping is enabled
};

